package com.hygj.controller;


import com.hygj.pojo.User;
import com.hygj.service.UserService;
import com.hygj.util.PageUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hygl
 */
@Controller
@RequestMapping
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 登录login验证-----验证用户名
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping("checkLogin")
	public void doLogin(HttpServletResponse response, String username) throws Exception {
		   System.out.println("控制器--");
		   boolean flag= userService.findUserByLoginName(username);
		    PrintWriter out = response.getWriter();
		    out.write(flag+"");
		     System.out.println(flag);
		    out.close();
	}


	/**
	 * 查询全部
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getLoginAll")
	public String getLoginAll(Map<String, Object> map, HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		PageUtil<User> page = userService.page(request, 10, params);
		map.put("page", page);
		return "logins/registerShow";
	}
    
	/**
	 * 删除
	 * @RequestParam("id")其中id是
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteLogin")
	public String deleteLogin(@RequestParam("id") Integer id) {
		userService.delete("delete", id);
		return "redirect:getUserWithRoleAll";
	}
	
	
	/**
	 * 删除，多项删除 @RequestParam("inform")引入 选择框 inform
	 * ===><input type="checkbox" name="inform">
	 * @return
	 */
	@RequestMapping("delete_user")
	public String delete_user(@RequestParam("inform") Integer[] ids){
		userService.delete(ids);
		return "redirect:getUserWithRoleAll";
	}


	/**
	 * 退出
	 * 
	 * @return
	 */
	@RequestMapping("logout")
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "login";
	}

}
